package com.heima.Serivce.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.heima.Mapper.WmSensitiveMapper;
import com.heima.Serivce.WemediaSensitiveSerivce;
import com.heima.model.Admin.Dtos.SensitiveDto;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.wemedia.pojos.WmSensitive;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.List;


@Service
@Slf4j
@Transactional
public class WemediaSensitiveService implements WemediaSensitiveSerivce {
    @Autowired
    WmSensitiveMapper wmSensitiveMapper;

    @Override
    public ResponseResult DeleteSensitive(Integer id) {
        if(id==null)
        {
            return ResponseResult.errorResult(404,"数据不存在");
        }
        int delete = wmSensitiveMapper.deleteById(id);
        return ResponseResult.okResult(delete);
    }

    @Override
    public ResponseResult SensitiveLIst(SensitiveDto sensitiveDto) {
        //        获取敏感词
        QueryWrapper<WmSensitive> wrapper = new QueryWrapper<>();
//        按时间倒序排序
        wrapper.orderByDesc("created_time");
        IPage<WmSensitive> page=new Page<>();
        page.setPages(sensitiveDto.getPage());
        page.setSize(sensitiveDto.getSize());
//        模糊查询
        if(sensitiveDto.getName()!=null)
        wrapper.like("sensitives",sensitiveDto.getName());
//        分页查询
        IPage<WmSensitive> iPage = wmSensitiveMapper.selectPage(page, wrapper);
        List<WmSensitive> sensitives = iPage.getRecords();
        return ResponseResult.okResult(sensitives);
    }

    @Override
    public ResponseResult SaveSensitive(WmSensitive wmSensitive) {

        QueryWrapper<WmSensitive> wrapper=new QueryWrapper<>();
        wrapper.eq("sensitives",wmSensitive.getSensitives());
        WmSensitive one = wmSensitiveMapper.selectOne(wrapper);
        if(one!=null)
            return ResponseResult.errorResult(201,"数据已经存在");

        int insert = wmSensitiveMapper.insert(wmSensitive);
        return ResponseResult.okResult(insert);
    }

    @Override
    public ResponseResult UpdateSensitive(WmSensitive wmSensitive) {
        WmSensitive sensitive = wmSensitiveMapper.selectById(wmSensitive.getId());
        if(sensitive==null)
            return ResponseResult.errorResult(404,"数据不存在");
        int update = wmSensitiveMapper.updateById(wmSensitive);
        log.info("Sensitive修改成功");
        return ResponseResult.okResult(update);
    }
}
